package cn.rwklyd.ecommerce.mapper;

import cn.rwklyd.ecommerce.pojo.Cart;
import cn.rwklyd.ecommerce.pojo.Product;
import cn.rwklyd.ecommerce.pojo.Users;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 键盘书生
 * @date 2024/12/2
 */
@Mapper
public interface UserMapper {
    //根据用户名查询用户信息
    @Select("SELECT * FROM users WHERE username = #{username}")
    Users getUserByName(String username);

    //用户ID查询用户信息
    @Select("SELECT * FROM users WHERE id = #{id}")
    Users getUserById(String id);

    //用户信息更新
    @Update("UPDATE users SET username = #{username}, password = #{password}, email = #{email}, role = #{role}, balance = #{balance} WHERE id = #{id}")
    int updateUser(Users user);

    //用户添加商品到购物车
    @Insert("INSERT INTO cart (user_id, product_id, quantity, total_price) VALUES (#{userId}, #{productId}, #{quantity}, #{totalPrice})")
    void addProductToCart(String userId,String productId,Integer quantity,Double totalPrice);

    //获取用户添加购物车中的商品信息
    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND is_pay=0")
    List<Cart> getCartList(String userId);

    //根据ID和用户id查询购物车信息
    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND id = #{cartId}")
    Cart getCartById(String userId,Long cartId);

    //支付成功，将购物车商品支付状态改为已经支付
    @Update("UPDATE cart SET is_pay = 1 WHERE user_id = #{userId} AND id = #{cartId}")
    int updateCartPayStatus(String userId,Long cartId);

    //查看用户历史订单
    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND is_pay = 1")
    List<Cart> getHistoryOrder(String userId);

    //删除历史订单信息
    @Delete("DELETE FROM cart WHERE id = #{cartId} AND user_id = #{userId} AND is_pay = 1")
    int deleteHistoryOrder(Long cartId, String userId);

    //根据商品id查询商品信息
    @Select("SELECT * FROM products WHERE product_id = #{productId}")
    Product getProductById(String productId);

    //更新商品库存
    @Update("UPDATE products SET stock = #{stock} WHERE product_id = #{productId}")
    int updateProductStock(Integer productId,Integer stock);



}
